package me.xiaoyan.java.chapter15.shopping;

import java.util.Scanner;

public class Shop {
	Goods[] store = new Goods[5];
	Scanner input = new Scanner(System.in);
	public void initGoods(){
		Goods g1 = new Goods();
		g1.setValue(1, "电视", 3999);
		store[0] = g1;
		Goods g2 = new Goods();
		g2.setValue(2, "洗衣机", 2999);
		store[1] = g2;
		Goods g3 = new Goods();
		g3.setValue(3, "冰箱",2409);
		store[2] = g3;
		Goods g4 = new Goods();
		g4.setValue(4, "电饭煲", 369);
		store[3] = g4;
		Goods g5 = new Goods();
		g5.setValue(5, "电风扇", 199);
		store[4] = g5;
	}
	
	public void show(){
		System.out.println("*************欢迎进入购物商店*****************");
		System.out.println("\t编号\t名称\t价格");
		for (int i = 0; i < store.length; i++) {
			store[i].showInfo();
		}
		System.out.println("***************************************");
	}
	
	/**
	 * 购买商品
	 */
	public void buy(){
		System.out.print("请输入批发商品编号:");
		int goodsSn = input.nextInt();
		if(goodsSn < 1 || goodsSn > store.length)
		{//验证输入的商品编号是否错误(下标越界)
			buy();//重新走该流程
		}
		Goods g = store[goodsSn-1];//获取购买的商品对象
		System.out.print("请输入批发商品编号:");
		int buyCount = input.nextInt();//获取购买数量
		
		
		//计算总金额
		String amount = change(g.price * buyCount).toString();
		System.out.println("应付总金额:￥" + amount);
	}
	
	/**
	 * 	显示格式转换，每隔三位加逗号 
	 * @param d
	 */
	public StringBuffer change(double d){
		StringBuffer str=new StringBuffer(String.valueOf(d));		
		for(int i=str.indexOf(".")-3;i>0;i=i-3){
			str.insert(i,',');
		}
		return str;
	}

	public static void main(String[] args) {
		User usr = new User();
		usr.checkLoginInfo();//检测是否登录，如果没有登录，请登录
		
		Shop shop = new Shop();//创建一个商店
		shop.initGoods();//初始化商店商品
		shop.show();
		shop.buy();
	}

}
